//
//  BaseTableView.m
//  JKWDTeacher
//
//  Created by 学大教育 on 2022/1/16.
//

#import "BaseTableView.h"

@implementation BaseTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}
- (void)setSourceData:(NSArray *)sourceData
{
    _sourceData = sourceData;
    [self reloadData];
}
#pragma mark - UITableViewDelegate & UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _isMultiSection ? self.sourceData.count : 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[section];
        count = sectionModel.cells.count;
    }else{
        count = self.sourceData.count;
    }
    return count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellModel *model = nil;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[indexPath.section];
        model = sectionModel.cells[indexPath.row];
    }else{
        model = self.sourceData[indexPath.row];
    }
    BaseTableViewCell *cell = [BaseTableViewCell cellForModel:model tableView:tableView];
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellModel *model = nil;
    if (_isMultiSection) {
        BaseSectionModel *sectionModel =  self.sourceData[indexPath.section];
        model = sectionModel.cells[indexPath.row];
    }else{
        model = self.sourceData[indexPath.row];
    }
    return model.cellHeight ?: UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] init];
    return headerView;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc] init];
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(tableView:didselectIndexPath:)]) {
        [_cellDelegate tableView:tableView didselectIndexPath:indexPath];
    }
}

#pragma mark - cell Action
-(void)actionIndexPath:(NSIndexPath *)indexPath infoData:(nullable id)data
{
    if (self.cellDelegate && [self.cellDelegate respondsToSelector:@selector(tableView:didActionIndexPath:infoData:)]) {
        [self.cellDelegate tableView:self didActionIndexPath:indexPath infoData:data];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
